#include "tieta_v4.h"

#include <stdio.h>
#include <string.h>

#include "../../windows_service/tmodbus/tmodbus.h"

#define switch_table_len      52
#define register_table_len    63
#define modbus_dev_table_size 1
#define sendbuffer_length     256

#define DEVICE_ID "BT106002004TTNY200224002"

/***************************************************************************************************
 * *****************************************静态函数声明****************************************** *
 ***************************************************************************************************/

/***************************************************************************************************
 * *****************************************静态函数定义****************************************** *
 ***************************************************************************************************/
static uint8_t sendbuffer[sendbuffer_length];

/***************************************************************************************************
 * ***************************************开关量表变量定义**************************************** *
 ***************************************************************************************************/
static bool reserve_1_switch_s              = 0;
static bool cell_pressure_difference_high_s = 1;
static bool charge_overcurrent_s            = 0;
static bool discharge_overcurrent_s         = 0;
static bool short_circuit_protection_s      = 1;
static bool charge_temp_high_s              = 0;
static bool discharge_temp_high_s           = 0;
static bool charge_temp_low_s               = 0;

static bool discharge_temp_low_s               = 0;
static bool charging_mos_failure_s             = 0;
static bool discharge_mos_failure_s            = 0;
static bool internal_communication_exception_s = 1;
static bool cell_overcharge_voltage_1_s        = 0;
static bool cell_overcharge_voltage_2_s        = 0;
static bool cell_overcharge_voltage_3_s        = 0;
static bool cell_overcharge_voltage_4_s        = 0;

static bool cell_overcharge_voltage_5_s  = 1;
static bool cell_overcharge_voltage_6_s  = 0;
static bool cell_overcharge_voltage_7_s  = 0;
static bool cell_overcharge_voltage_8_s  = 1;
static bool cell_overcharge_voltage_9_s  = 0;
static bool cell_overcharge_voltage_10_s = 0;
static bool cell_overcharge_voltage_11_s = 1;
static bool cell_overcharge_voltage_12_s = 0;

static bool cell_overcharge_voltage_13_s = 0;
static bool cell_overcharge_voltage_14_s = 0;
static bool cell_overcharge_voltage_15_s = 0;
static bool cell_overcharge_voltage_16_s = 0;
static bool cell_overcharge_voltage_17_s = 0;
static bool cell_overcharge_voltage_18_s = 0;
static bool cell_overcharge_voltage_19_s = 0;
static bool cell_overcharge_voltage_20_s = 1;

static bool cell_overdischarge_voltage_1_s = 0;
static bool cell_overdischarge_voltage_2_s = 0;
static bool cell_overdischarge_voltage_3_s = 0;
static bool cell_overdischarge_voltage_4_s = 0;
static bool cell_overdischarge_voltage_5_s = 1;
static bool cell_overdischarge_voltage_6_s = 0;
static bool cell_overdischarge_voltage_7_s = 0;
static bool cell_overdischarge_voltage_8_s = 0;

static bool cell_overdischarge_voltage_9_s  = 0;
static bool cell_overdischarge_voltage_10_s = 0;
static bool cell_overdischarge_voltage_11_s = 1;
static bool cell_overdischarge_voltage_12_s = 0;
static bool cell_overdischarge_voltage_13_s = 0;
static bool cell_overdischarge_voltage_14_s = 0;
static bool cell_overdischarge_voltage_15_s = 0;
static bool cell_overdischarge_voltage_16_s = 0;

static bool cell_overdischarge_voltage_17_s = 1;
static bool cell_overdischarge_voltage_18_s = 0;
static bool cell_overdischarge_voltage_19_s = 0;
static bool cell_overdischarge_voltage_20_s = 1;

/***************************************************************************************************
 * ***************************************寄存器表变量定义**************************************** *
 ***************************************************************************************************/
static uint16_t total_battery_pack_voltage_s          = 0;
static uint16_t cells_number_s                        = 0;
static uint16_t soc_s                                 = 0;
static uint16_t residual_capacity_s                   = 0;
static uint16_t soh_s                                 = 0;
static uint16_t charging_current_s                    = 0;
static uint16_t ambient_temperature_s                 = 0;
static uint16_t minimum_cell_temperature_s            = 0;
static uint16_t mos_temperature_s                     = 0;
static uint16_t cell_voltage_1_s                      = 0;
static uint16_t cell_voltage_2_s                      = 0;
static uint16_t cell_voltage_3_s                      = 0;
static uint16_t cell_voltage_4_s                      = 0;
static uint16_t cell_voltage_5_s                      = 0;
static uint16_t cell_voltage_6_s                      = 0;
static uint16_t cell_voltage_7_s                      = 0;
static uint16_t cell_voltage_8_s                      = 0;
static uint16_t cell_voltage_9_s                      = 0;
static uint16_t cell_voltage_10_s                     = 0;
static uint16_t cell_voltage_11_s                     = 0;
static uint16_t cell_voltage_12_s                     = 0;
static uint16_t cell_voltage_13_s                     = 0;
static uint16_t cell_voltage_14_s                     = 0;
static uint16_t cell_voltage_15_s                     = 0;
static uint16_t cell_voltage_16_s                     = 0;
static uint16_t cell_voltage_17_s                     = 0;
static uint16_t cell_voltage_18_s                     = 0;
static uint16_t cell_voltage_19_s                     = 0;
static uint16_t cell_voltage_20_s                     = 0;
static uint16_t maximum_cell_temperature_s            = 0;
static uint16_t reserve_1_s                           = 0;
static uint16_t reserve_2_s                           = 0;
static uint16_t mos_status_s                          = 0;
static uint16_t reserve_3_s                           = 0;
static uint16_t reserve_4_s                           = 0;
static uint16_t reserve_5_s                           = 0;
static uint16_t reserve_6_s                           = 0;
static uint16_t reserve_7_s                           = 0;
static uint16_t reserve_8_s                           = 0;
static uint16_t reserve_9_s                           = 0;
static uint16_t battery_version_s                     = 0;
static uint16_t charge_current_request_value_s        = 0;
static uint16_t charge_voltage_request_value_s        = 0;
static uint16_t bms_charging_flag_s                   = 0;
static uint16_t fast_charging_current_request_value_s = 0;
static uint16_t fast_charging_voltage_request_value_s = 0;
static uint16_t bms_fast_charge_charging_flag_s       = 0;
static uint16_t device_id_1_s                         = 0;
static uint16_t device_id_2_s                         = 0;
static uint16_t device_id_3_s                         = 0;
static uint16_t device_id_4_s                         = 0;
static uint16_t device_id_5_s                         = 0;
static uint16_t device_id_6_s                         = 0;
static uint16_t device_id_7_s                         = 0;
static uint16_t device_id_8_s                         = 0;
static uint16_t device_id_9_s                         = 0;
static uint16_t device_id_10_s                        = 0;
static uint16_t device_id_11_s                        = 0;
static uint16_t device_id_12_s                        = 0;
static uint16_t device_id_13_s                        = 0;
static uint16_t device_id_14_s                        = 0;
static uint16_t reserve_10_s                          = 0;
static uint16_t reserve_11_s                          = 0;

/* 修改switch_table的时候，需要对应修改switch_table_size大小 */
static modbus_switch_table_value_t switch_table[switch_table_len] = {
    {"reserve", 0, &reserve_1_switch_s, true},                                           /* 保留位 */
    {"cell_pressure_difference_high", 1, &cell_pressure_difference_high_s, true},        /* 电芯压差过大保护 */
    {"charge_overcurrent", 2, &charge_overcurrent_s, true},                              /* 充电过流保护 */
    {"discharge_overcurrent", 3, &discharge_overcurrent_s, true},                        /* 放电过流保护 */
    {"short_circuit_protection", 4, &short_circuit_protection_s, true},                  /* 短路保护 */
    {"charge_temp_high", 5, &charge_temp_high_s, true},                                  /* 充电高温保护 */
    {"discharge_temp_high", 6, &discharge_temp_high_s, true},                            /* 放电高温保护 */
    {"charge_temp_low", 7, &charge_temp_low_s, true},                                    /* 充电低温保护 */
    {"discharge_temp_low", 8, &discharge_temp_low_s, true},                              /* 放电低温保护 */
    {"charging_mos_failure", 9, &charging_mos_failure_s, true},                          /* 充电MOS损坏 */
    {"discharge_mos_failure", 10, &discharge_mos_failure_s, true},                       /* 放电MOS损坏 */
    {"internal_communication_exception", 11, &internal_communication_exception_s, true}, /* 内部通讯异常 */

    /* 过充电压保护猜测是单体过充电压保护，序号对应着单体号 */
    {"cell_overcharge_voltage_1", 12, &cell_overcharge_voltage_1_s, true},   /* 过充电压保护1 */
    {"cell_overcharge_voltage_2", 13, &cell_overcharge_voltage_2_s, true},   /* 过充电压保护2 */
    {"cell_overcharge_voltage_3", 14, &cell_overcharge_voltage_3_s, true},   /* 过充电压保护3 */
    {"cell_overcharge_voltage_4", 15, &cell_overcharge_voltage_4_s, true},   /* 过充电压保护4 */
    {"cell_overcharge_voltage_5", 16, &cell_overcharge_voltage_5_s, true},   /* 过充电压保护5 */
    {"cell_overcharge_voltage_6", 17, &cell_overcharge_voltage_6_s, true},   /* 过充电压保护6 */
    {"cell_overcharge_voltage_7", 18, &cell_overcharge_voltage_7_s, true},   /* 过充电压保护7 */
    {"cell_overcharge_voltage_8", 19, &cell_overcharge_voltage_8_s, true},   /* 过充电压保护8 */
    {"cell_overcharge_voltage_9", 20, &cell_overcharge_voltage_9_s, true},   /* 过充电压保护9 */
    {"cell_overcharge_voltage_10", 21, &cell_overcharge_voltage_10_s, true}, /* 过充电压保护10 */
    {"cell_overcharge_voltage_11", 22, &cell_overcharge_voltage_11_s, true}, /* 过充电压保护11 */
    {"cell_overcharge_voltage_12", 23, &cell_overcharge_voltage_12_s, true}, /* 过充电压保护12 */
    {"cell_overcharge_voltage_13", 24, &cell_overcharge_voltage_13_s, true}, /* 过充电压保护13 */
    {"cell_overcharge_voltage_14", 25, &cell_overcharge_voltage_14_s, true}, /* 过充电压保护14 */
    {"cell_overcharge_voltage_15", 26, &cell_overcharge_voltage_15_s, true}, /* 过充电压保护15 */
    {"cell_overcharge_voltage_16", 27, &cell_overcharge_voltage_16_s, true}, /* 过充电压保护16 */
    {"cell_overcharge_voltage_17", 28, &cell_overcharge_voltage_17_s, true}, /* 过充电压保护17 */
    {"cell_overcharge_voltage_18", 29, &cell_overcharge_voltage_18_s, true}, /* 过充电压保护18 */
    {"cell_overcharge_voltage_19", 30, &cell_overcharge_voltage_19_s, true}, /* 过充电压保护19 */
    {"cell_overcharge_voltage_20", 31, &cell_overcharge_voltage_20_s, true}, /* 过充电压保护20 */

    /* 过放电压保护猜测是单体过放电压保护，序号对应着单体号 */
    {"cell_overdischarge_voltage_1", 32, &cell_overdischarge_voltage_1_s, true},   /* 过放电压保护1 */
    {"cell_overdischarge_voltage_2", 33, &cell_overdischarge_voltage_2_s, true},   /* 过放电压保护2 */
    {"cell_overdischarge_voltage_3", 34, &cell_overdischarge_voltage_3_s, true},   /* 过放电压保护3 */
    {"cell_overdischarge_voltage_4", 35, &cell_overdischarge_voltage_4_s, true},   /* 过放电压保护4 */
    {"cell_overdischarge_voltage_5", 36, &cell_overdischarge_voltage_5_s, true},   /* 过放电压保护5 */
    {"cell_overdischarge_voltage_6", 37, &cell_overdischarge_voltage_6_s, true},   /* 过放电压保护6 */
    {"cell_overdischarge_voltage_7", 38, &cell_overdischarge_voltage_7_s, true},   /* 过放电压保护7 */
    {"cell_overdischarge_voltage_8", 39, &cell_overdischarge_voltage_8_s, true},   /* 过放电压保护8 */
    {"cell_overdischarge_voltage_9", 40, &cell_overdischarge_voltage_9_s, true},   /* 过放电压保护9 */
    {"cell_overdischarge_voltage_10", 41, &cell_overdischarge_voltage_10_s, true}, /* 过放电压保护10 */
    {"cell_overdischarge_voltage_11", 42, &cell_overdischarge_voltage_11_s, true}, /* 过放电压保护11 */
    {"cell_overdischarge_voltage_12", 43, &cell_overdischarge_voltage_12_s, true}, /* 过放电压保护12 */
    {"cell_overdischarge_voltage_13", 44, &cell_overdischarge_voltage_13_s, true}, /* 过放电压保护13 */
    {"cell_overdischarge_voltage_14", 45, &cell_overdischarge_voltage_14_s, true}, /* 过放电压保护14 */
    {"cell_overdischarge_voltage_15", 46, &cell_overdischarge_voltage_15_s, true}, /* 过放电压保护15 */
    {"cell_overdischarge_voltage_16", 47, &cell_overdischarge_voltage_16_s, true}, /* 过放电压保护16 */
    {"cell_overdischarge_voltage_17", 48, &cell_overdischarge_voltage_17_s, true}, /* 过放电压保护17 */
    {"cell_overdischarge_voltage_18", 49, &cell_overdischarge_voltage_18_s, true}, /* 过放电压保护18 */
    {"cell_overdischarge_voltage_19", 50, &cell_overdischarge_voltage_19_s, true}, /* 过放电压保护19 */
    {"cell_overdischarge_voltage_20", 51, &cell_overdischarge_voltage_20_s, true}, /* 过放电压保护20 */
};

/* 修改register_table的时候，需要对应修改register_table_size大小 */
static modbus_register_table_value_t register_table[register_table_len] = {
    {"total_battery_pack_voltage", 0, &total_battery_pack_voltage_s, true}, /* 电池包实际总电压 */
    {"cells_number", 1, &cells_number_s, true},                             /* 电芯数量 */
    {"soc", 2, &soc_s, true},                                               /* SOC */
    {"residual_capacity", 3, &residual_capacity_s, true},                   /* 剩余容量 */
    {"soh", 4, &soh_s, true},                                               /* SOH */
    {"charging_current", 5, &charging_current_s, true},                     /* 充电电流 */
    {"ambient_temperature", 6, &ambient_temperature_s, true},               /* 环境温度 */
    {"minimum_cell_temperature", 7, &minimum_cell_temperature_s, true},     /* 电芯最低温度 */
    {"mos_temperature", 8, &mos_temperature_s, true},                       /* 板卡温度（MOS管温度） */

    {"cell_voltage_1", 9, &cell_voltage_1_s, true},    /* 电芯电压1 */
    {"cell_voltage_2", 10, &cell_voltage_2_s, true},   /* 电芯电压2 */
    {"cell_voltage_3", 11, &cell_voltage_3_s, true},   /* 电芯电压3 */
    {"cell_voltage_4", 12, &cell_voltage_4_s, true},   /* 电芯电压4 */
    {"cell_voltage_5", 13, &cell_voltage_5_s, true},   /* 电芯电压5 */
    {"cell_voltage_6", 14, &cell_voltage_6_s, true},   /* 电芯电压6 */
    {"cell_voltage_7", 15, &cell_voltage_7_s, true},   /* 电芯电压7 */
    {"cell_voltage_8", 16, &cell_voltage_8_s, true},   /* 电芯电压8 */
    {"cell_voltage_9", 17, &cell_voltage_9_s, true},   /* 电芯电压9 */
    {"cell_voltage_10", 18, &cell_voltage_10_s, true}, /* 电芯电压10 */
    {"cell_voltage_11", 19, &cell_voltage_11_s, true}, /* 电芯电压11 */
    {"cell_voltage_12", 20, &cell_voltage_12_s, true}, /* 电芯电压12 */
    {"cell_voltage_13", 21, &cell_voltage_13_s, true}, /* 电芯电压13 */
    {"cell_voltage_14", 22, &cell_voltage_14_s, true}, /* 电芯电压14 */
    {"cell_voltage_15", 23, &cell_voltage_15_s, true}, /* 电芯电压15 */
    {"cell_voltage_16", 24, &cell_voltage_16_s, true}, /* 电芯电压16 */
    {"cell_voltage_17", 25, &cell_voltage_17_s, true}, /* 电芯电压17 */
    {"cell_voltage_18", 26, &cell_voltage_18_s, true}, /* 电芯电压18 */
    {"cell_voltage_19", 27, &cell_voltage_19_s, true}, /* 电芯电压19 */
    {"cell_voltage_20", 28, &cell_voltage_20_s, true}, /* 电芯电压20 */

    {"maximum_cell_temperature", 29, &maximum_cell_temperature_s, true},                       /* 电芯最高温度 */
    {"reserve", 30, &reserve_1_s, true},                                                       /* 保留位 */
    {"reserve", 31, &reserve_2_s, true},                                                       /* 保留位 */
    {"mos_status", 32, &mos_status_s, true},                                                   /* MOS状态 */
    {"reserve", 33, &reserve_3_s, true},                                                       /* 保留位 */
    {"reserve", 34, &reserve_4_s, true},                                                       /* 保留位 */
    {"reserve", 35, &reserve_5_s, true},                                                       /* 保留位 */
    {"reserve", 36, &reserve_6_s, true},                                                       /* 保留位 */
    {"reserve", 37, &reserve_7_s, true},                                                       /* 保留位 */
    {"reserve", 38, &reserve_8_s, true},                                                       /* 保留位 */
    {"reserve", 39, &reserve_9_s, true},                                                       /* 保留位 */
    {"battery_version", 40, &battery_version_s, true},                                         /* 电池版本 */
    {"charge_current_request_value", 41, &charge_current_request_value_s, true},               /* 充电电流请求值 */
    {"charge_voltage_request_value", 42, &charge_voltage_request_value_s, true},               /* 充电电压请求值 */
    {"bms_charging_flag", 43, &bms_charging_flag_s, true},                                     /* BMS充电标志位 */
    {"fast_charging_current_request_value", 44, &fast_charging_current_request_value_s, true}, /* 快充充电电流请求值 */
    {"fast_charging_voltage_request_value", 45, &fast_charging_voltage_request_value_s, true}, /* 快充充电电压请求值 */
    {"bms_fast_charge_charging_flag", 46, &bms_fast_charge_charging_flag_s, true},             /* BMS 快充充电标志位 */

    {"device_id_1", 1000, &device_id_1_s, true},   /* 设备ID (1) */
    {"device_id_2", 1001, &device_id_2_s, true},   /* 设备ID (2) */
    {"device_id_3", 1002, &device_id_3_s, true},   /* 设备ID (3) */
    {"device_id_4", 1003, &device_id_4_s, true},   /* 设备ID (4) */
    {"device_id_5", 1004, &device_id_5_s, true},   /* 设备ID (5) */
    {"device_id_6", 1005, &device_id_6_s, true},   /* 设备ID (6) */
    {"device_id_7", 1006, &device_id_7_s, true},   /* 设备ID (7) */
    {"device_id_8", 1007, &device_id_8_s, true},   /* 设备ID (8) */
    {"device_id_9", 1008, &device_id_9_s, true},   /* 设备ID (9) */
    {"device_id_10", 1009, &device_id_10_s, true}, /* 设备ID (10) */
    {"device_id_11", 1010, &device_id_11_s, true}, /* 设备ID (11) */
    {"device_id_12", 1011, &device_id_12_s, true}, /* 设备ID (12) */
    {"device_id_13", 1012, &device_id_13_s, true}, /* 设备ID (13) */
    {"device_id_14", 1013, &device_id_14_s, true}, /* 设备ID (14) */
    {"reserve", 1014, &reserve_10_s, true},        /* 保留 */
    {"reserve", 1015, &reserve_11_s, true},        /* 保留 */
};

static void data_printf(uint8_t* data, uint8_t data_len) {
  for (size_t i = 0; i < data_len; i++) {
    printf("0X%x ", data[i]);
  }
  printf("\n");
}

static modbus_data_table_t modbus_dev_1_data_table = {switch_table, switch_table_len,       // 开关量表相关
                                                      register_table, register_table_len};  // 寄存器表相关

static modbus_dev_table_t modbus_dev_table[modbus_dev_table_size] = {
    {1, &modbus_dev_1_data_table, sendbuffer, sendbuffer_length, modbus_frame_max_len},
};

void tieta_init(void) { modbus_dev_reg(modbus_dev_table, modbus_dev_table_size); }

void tieta_receive(uint8_t* data, uint8_t data_len) {
  int16_t send_len;
  send_len = modbus_receive(data, data_len);
  if (send_len > 0) {
    data_printf(sendbuffer, send_len);
  }
}

void register_table_device_id_set(void) {
  int32_t register_table_dev_id_start_index;
  register_table_dev_id_start_index = modbus_find_register_table_index_from_data_address(1, 1000);
  for (size_t index = 0; index < strlen(DEVICE_ID); index += 2) {
    *register_table[register_table_dev_id_start_index++].data = (DEVICE_ID[index] << 8) | (DEVICE_ID[index + 1] << 0);
  }
}